package com.foreknow.demo15;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PrepareStatementDemo {
    public static void main(String[] args) {
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取链接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wjy?useUnicode=true&characterEncoding=utf8&useSSL=true","root","roat");
            //3.获取PrepareStatement
            PreparedStatement pstmt = conn.prepareStatement("insert into employee (username,password) values(?,?)");
            //将?替换成具体的值
            pstmt.setString(1,"lily");
            pstmt.setString(2,"lilylilylily");
            // 执行 SQL语句
            int isRight = pstmt.executeUpdate();
            System.out.println(isRight);
//            List<Employee> list = new ArrayList<>();
//            while (rs.next()) {
//                int id = rs.getInt("id");
//                String username = rs.getString("username");
//                String password = rs.getString("password");
//                Employee emp = new Employee();
//                emp.setId(id);
//                emp.setUsername(username);
//                emp.setPassword(password);
//                list.add(emp);
//            }
//            for (Employee e:list) {
//                System.out.println(e.getUsername());
//            }
//            rs.close();
            pstmt.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
